﻿using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Core.Service;
using MRGSP.SurveyMaster.Core.Service.Utils;

namespace MRGSP.SurveyMaster.Service.Utils
{
    public class SurveyBuilder : ISurveyBuilder
    {
        private readonly ITreeBuilder treeBuilder;

        public SurveyBuilder(ITreeBuilder treeBuilder)
        {
            this.treeBuilder = treeBuilder;
        }

        public string Build(IEnumerable<Question> questions)
        {
            var roots = treeBuilder.Build(questions);
            return roots.Aggregate(string.Empty, (current, root) 
                 => current + QuestionBuilder.Get(root.Type).Build(root));
        }
    }
}